﻿/*
 * ================================================================================
 *          File: PrintBLL.cs
 *          Desc:小票打印类
 *          Auth:JF
 *          Date:2011-10-21
 *================================================================================
 *          Change History
 *================================================================================
 *          Date            Author          Description
 *          --------        --------        ----------------
 *          
 * 
 *================================================================================
 */

using System;
using System.Collections.Generic;
using System.Text;

using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using EShop.Common;
using EShop.Model;

namespace PrintBLL
{
    public class Print
    {
        private int _PrintAreaWidth = 58;

        private int _PrintAreaHeight = 600;

        private Font _TopTitleFont = new Font("Arial", 8, FontStyle.Bold);

        private Color _TopTitleColor = Color.Black;

        private Point _TopTitleLocate = new Point(15,3);

        private string _TopTitleText = "COLOUR(颜)欧美护肤产品";

        private int _TopTitleRowSpacing = 15;//顶部标题与下一行的行距

        private int _DocumentRowSpacing = 16;//内容每行行距

        private int _DocumentLeftSpaceBetween = 2;//内容左边间距

        private Font _DocumentFont = new Font("Arial", 8);

        private Color _DocumentColor = Color.Black;

        private List<string> _DrawString = new List<string>();

        private string _EmployeeID = string.Empty;
        /// <summary>
        /// 员工工号
        /// </summary>
        public string EmployeeID
        {
            get { return _EmployeeID; }
            set { _EmployeeID = value; }
        }

        private string _OrderID = string.Empty;
        /// <summary>
        /// 订单号
        /// </summary>
        public string OrderID
        {
            get { return _OrderID; }
            set { _OrderID = value; }
        }

        private decimal _PayFact = 0;
        /// <summary>
        /// 应收
        /// </summary>
        public decimal PayFact
        {
            get { return _PayFact; }
            set { _PayFact = value; }
        }

        private decimal _PayCash = 0;
        /// <summary>
        /// 已付现金
        /// </summary>
        public decimal PayCash
        {
            get { return _PayCash; }
            set { _PayCash = value; }
        }

        private bool _IsShowPrintDialog = false;
        /// <summary>
        /// 是否显示打印预览
        /// </summary>
        public bool IsShowPrintDialog
        {
            get { return _IsShowPrintDialog; }
            set { _IsShowPrintDialog = value; }
        }

        private decimal _PayReturn
        {
            get { return _PayCash - _PayFact; }
        }

        private Margins _DocumentMargings = new Margins(20, 20, 20, 20);
        /// <summary>
        /// 打印Margins
        /// </summary>
        public Margins DocumentMargings
        {
            get { return _DocumentMargings; }
            set { _DocumentMargings = value; }
        }

        private Bill _bill = new Bill();

        public Bill Bill
        {
            get { return _bill; }
            set { _bill = value; }
        }

        public Print() { }
        public Print(Bill bill)
        {
            this._bill = bill;
        }

        public void DrawString(string _DrawText)
        {
            this._DrawString.Add(_DrawText);
        }

        public void DoPrint()
        {
            PrintPreviewDialog _PrintPreview = new PrintPreviewDialog();
            PrintDocument _PrintDocument = new PrintDocument();
            int height = 2;
            height += (int)(_TopTitleRowSpacing + (_DocumentRowSpacing * 3));//顶部总高度
            height += _DrawString.Count * (int)_DocumentRowSpacing;//文档总距离
            height += (int)_DocumentRowSpacing * 5;//底部总高度
            this._PrintAreaHeight = height;
            _PrintDocument.DefaultPageSettings.PaperSize = new PaperSize("Print Area", getYc(_PrintAreaWidth), _PrintAreaHeight + _DocumentMargings.Top + _DocumentMargings.Bottom);
            _PrintDocument.DefaultPageSettings.Margins = _DocumentMargings;
            _PrintDocument.PrintPage += new PrintPageEventHandler(DoPrintEvent);
            _PrintPreview.Document = _PrintDocument;
            if (_IsShowPrintDialog) _PrintPreview.ShowDialog();
            try
            { _PrintDocument.Print(); }
            catch (Exception ex)
            {
                log.Write("打印失败:"+ex.Message);
                MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                _PrintDocument.PrintController.OnEndPrint(_PrintDocument, new PrintEventArgs());
            }
        }

        private void DoPrintEvent(object sender, PrintPageEventArgs e)
        {
            int _LocateY = 2;
            e.Graphics.DrawString(_TopTitleText, _TopTitleFont, new SolidBrush(_TopTitleColor), _TopTitleLocate);
            _LocateY += _DocumentRowSpacing;
            e.Graphics.DrawString("工号:" + _EmployeeID + "  单号:" + _bill.OrderID, _DocumentFont, new SolidBrush(_DocumentColor), _DocumentLeftSpaceBetween, _LocateY);
            _LocateY += _DocumentRowSpacing;
            e.Graphics.DrawString("品名         数量       单价      金额", _DocumentFont, new SolidBrush(_DocumentColor), _DocumentLeftSpaceBetween, _LocateY);
            _LocateY += _DocumentRowSpacing;
            e.Graphics.DrawString("----------------------------------------------", _DocumentFont, new SolidBrush(_DocumentColor), _DocumentLeftSpaceBetween, _LocateY);
            _LocateY += _DocumentRowSpacing;
            foreach (string strItem in _DrawString)
            {
                e.Graphics.DrawString(strItem, _DocumentFont, new SolidBrush(_DocumentColor), _DocumentLeftSpaceBetween, _LocateY);
                _LocateY += _DocumentRowSpacing;
            }
            e.Graphics.DrawString("----------------------------------------------", _DocumentFont, new SolidBrush(_DocumentColor), _DocumentLeftSpaceBetween, _LocateY);
            _LocateY += _DocumentRowSpacing;
            e.Graphics.DrawString("应收" + _bill.PayFact, _DocumentFont, new SolidBrush(_DocumentColor), _DocumentLeftSpaceBetween, _LocateY);
            _LocateY += _DocumentRowSpacing;
            e.Graphics.DrawString("现金" + _bill.PayReal.ToString() + "          找零" + _bill.ReturnMoney.ToString(), _DocumentFont, new SolidBrush(_DocumentColor), _DocumentLeftSpaceBetween, _LocateY);
            _LocateY += _DocumentRowSpacing;
            e.Graphics.DrawString("请妥善保管小票", _DocumentFont, new SolidBrush(_DocumentColor), _DocumentLeftSpaceBetween, _LocateY);
            _LocateY += _DocumentRowSpacing;
            e.Graphics.DrawString(DateTime.Now.ToString("yy/MM/dd hh:mm:ss"), _DocumentFont, new SolidBrush(_DocumentColor), _DocumentLeftSpaceBetween, _LocateY);
            e.Graphics.DrawRectangle(new Pen(Color.Black), new Rectangle(0, 0, getYc(58) - 15, _PrintAreaHeight));
        }

        private int getYc(double cm)
        {
            return (int)((cm / 25.4) * 100);
        }

        //private 
        //public Print(string 
    }

    
}
